package com.smyh.protcol;

import org.apache.mina.core.buffer.IoBuffer;

import com.smyh.cache.PreferenceCommon;
import com.smyh.cache.PreferenceSearcheParam;
import com.smyh.util.AddCrc;

public class CMDUtil {


	/**
	 * 查询本地参数
	 * @return  非101
	 */
	public static IoBuffer askLocalParam(byte[] phAddrBytes,byte askSubset){
		IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
		byte dataLength = 0x16;
		byte numP = 0x1;
		byte[] searchParamBytes5 = getSearchParam();
		byte[] spaceBytes = {0,0,0,0};

		buffer.put(ProtcolStruts.START_SIGN).put(dataLength).put(ProtcolStruts.LOCAL_ASK_CMD)
				.put(numP).put(searchParamBytes5).put(phAddrBytes).put(askSubset).put(spaceBytes).flip();
		byte sumCrc = AddCrc.getCrc(buffer, 2, buffer.limit()-1);

		IoBuffer sendBuffer = IoBuffer.allocate(0).setAutoExpand(true);
		/**
		 * 下发的数据需要加：FF
		 */
		sendBuffer.put((byte) 0xff).put(buffer).put(sumCrc).put(ProtcolStruts.END_SIGN).flip();

		return sendBuffer;
	}
	/**
	 *
	 * @param phAddrBytes-物理地址
	 * @param callTimes-呼叫时间
	 * @param waitTimes-等待时间
	 * @param callDistance-呼叫距离
	 * @param freqChanel-呼叫频段
	 * @param commFreqChanel-通讯频段
	 * @param paramContent-20个字节参数内容（第一个字节是标识）
	 * @return 非101
	 */
	public static IoBuffer setLocalParam(byte[] phAddrBytes,byte[] paramContent){
		IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
		byte dataLength = 0x2f;
		byte numP = 0x1;
		byte[] searchParamBytes5 = getSearchParam();
		byte[] spaceBytes9 = {0,0,0,0,0,0,0,0,0};
		byte[] spaceBytes5 = {0,0,0,0,0};

		buffer.put(ProtcolStruts.START_SIGN).put(dataLength).put(ProtcolStruts.LOCAL_PARAM_SET_CMD)
				.put(numP).put(searchParamBytes5).put(spaceBytes9).put(paramContent).put(spaceBytes5).flip();
		byte sumCrc = AddCrc.getCrc(buffer, 2, buffer.limit()-1);

		IoBuffer sendBuffer = IoBuffer.allocate(0).setAutoExpand(true);
		/**
		 * 下发的数据需要加：FF
		 */
		sendBuffer.put((byte) 0xff).put(buffer).put(sumCrc).put(ProtcolStruts.END_SIGN).flip();

		return sendBuffer;
	}

	/**
	 * 整定终端参数方法，返回字节指令
	 * @param phAddrBytes-物理地址
	 * @param callTimes-呼叫时间
	 * @param waitTimes-等待时间
	 * @param callDistance-呼叫距离
	 * @param freqChanel-呼叫频段
	 * @param commFreqChanel-通讯频段
	 * @param paramContent-20个字节参数内容（第一个字节是标识）
	 * @return 非101
	 */
	public static IoBuffer setDeviceParam(byte[] phAddrBytes,byte[] paramContent){
		IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
		byte dataLength = 0x2f;
		byte numP = 0x1;
		byte[] searchParamBytes5 = getSearchParam();
		byte[] spaceBytes5 = {0,0,0,0,0};

		buffer.put(ProtcolStruts.START_SIGN).put(dataLength).put(ProtcolStruts.DEVICE_PARAM_SET_CMD).put(numP)
				.put(searchParamBytes5).put(phAddrBytes).put(spaceBytes5).put(paramContent).put(spaceBytes5).flip();
		byte sumCrc = AddCrc.getCrc(buffer, 2, buffer.limit()-1);

		IoBuffer sendBuffer = IoBuffer.allocate(0).setAutoExpand(true);
		/**
		 * 下发的数据需要加：FF
		 */
		sendBuffer.put((byte) 0xff).put(buffer).put(sumCrc).put(ProtcolStruts.END_SIGN).flip();

		return sendBuffer;
	}

	/**
	 *
	 * @param callsParam 参数包含5字节：1-呼叫时间、2-等待时间、3-呼叫距离、4-呼叫频段、5-通讯频段
	 * @return 非101
	 */
	public static IoBuffer callDevicesParam(){
		IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
		byte dataLength = 0xd;
		byte numP = 0x1;
		byte[] searchParamBytes5 = getSearchParam();

		buffer.put(ProtcolStruts.START_SIGN).put(dataLength).put(ProtcolStruts.ALL_CALL_CMD).put(numP).put(searchParamBytes5).flip();

		byte sumCrc = AddCrc.getCrc(buffer, 2, buffer.limit()-1);
		IoBuffer sendBuffer = IoBuffer.allocate(0).setAutoExpand(true);
		/**
		 * 下发的数据需要加：FF
		 */
		sendBuffer.put((byte) 0xff).put(buffer).put(sumCrc).put(ProtcolStruts.END_SIGN).flip();

		return sendBuffer;
	}

	/**
	 * 查询终端参数
	 * @return 非101
	 */
	public static IoBuffer askDeviceParam(byte[] phAddrBytes,byte askSubset){
		IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
		byte dataLength = 0x16;
		byte numP = 0x1;
		byte[] searchParamBytes5 = getSearchParam();
		byte[] spaceBytes = {0,0,0,0};

		buffer.put(ProtcolStruts.START_SIGN).put(dataLength).put(ProtcolStruts.DEVICE_ASK_CMD)
				.put(numP).put(searchParamBytes5).put(phAddrBytes).put(askSubset).put(spaceBytes).flip();
		byte sumCrc = AddCrc.getCrc(buffer, 2, buffer.limit()-1);

		IoBuffer sendBuffer = IoBuffer.allocate(0).setAutoExpand(true);
		/**
		 * 下发的数据需要加：FF
		 */
		sendBuffer.put((byte) 0xff).put(buffer).put(sumCrc).put(ProtcolStruts.END_SIGN).flip();

		return sendBuffer;
	}


	/**
	 * 获取设备亮灯\熄灯指令：
	 * @param phAddrBytes--物理地址
	 * @param isOn --true亮灯、false熄灯
	 * @return 非101
	 */
	public static IoBuffer on_offLight(byte[] phAddrBytes,boolean isOn){
		IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
		byte dataLength = 0x16;
		byte[] on_off_cmd = null;
		if(isOn){
			on_off_cmd = ProtcolStruts.LIGHT_ON_CMD;
		}else{
			on_off_cmd = ProtcolStruts.LIGHT_OFF_CMD;
		}

		byte numP = 0x1;
		byte[] searchParamBytes5 = getSearchParam();
		byte[] spaceBytes = {0,0,0,0,0};

		buffer.put(ProtcolStruts.START_SIGN).put(dataLength).put(on_off_cmd)
				.put(numP).put(searchParamBytes5).put(phAddrBytes).put(spaceBytes).flip();
		byte sumCrc = AddCrc.getCrc(buffer, 2, buffer.limit()-1);

		IoBuffer sendBuffer = IoBuffer.allocate(0).setAutoExpand(true);
		/**
		 * 下发的数据需要加：FF
		 */
		sendBuffer.put((byte) 0xff).put(buffer).put(sumCrc).put(ProtcolStruts.END_SIGN).flip();

		return sendBuffer;
	}
	/**
	 * 和指定终端建立连接-断开连接：
	 * @param phAddrBytes--物理地址
	 * @param isOn --true建立、false断开
	 * @return 非101
	 */
	public static IoBuffer on_offConnect(byte[] phAddrBytes,boolean isOn){
		IoBuffer buffer = IoBuffer.allocate(0).setAutoExpand(true);
		byte dataLength = 0x16;
		byte[] on_off_cmd = null;
		if(isOn){
			on_off_cmd = ProtcolStruts.COMM_ON_CMD;
		}else{
			on_off_cmd = ProtcolStruts.COMM_OFF_CMD;
		}

		byte numP = 0x1;
		byte[] searchParamBytes5 = getSearchParam();
		byte[] spaceBytes = {0,0,0,0,0};

		buffer.put(ProtcolStruts.START_SIGN).put(dataLength).put(on_off_cmd)
				.put(numP).put(searchParamBytes5).put(phAddrBytes).put(spaceBytes).flip();
		byte sumCrc = AddCrc.getCrc(buffer, 2, buffer.limit()-1);

		IoBuffer sendBuffer = IoBuffer.allocate(0).setAutoExpand(true);
		/**
		 * 下发的数据需要加：FF
		 */
		sendBuffer.put((byte) 0xff).put(buffer).put(sumCrc).put(ProtcolStruts.END_SIGN).flip();

		return sendBuffer;
	}

	/**
	 * 获取搜索条件字节数组方法：
	 * @return
	 */
	private static byte[] getSearchParam(){
		byte callTimes = (byte) PreferenceCommon.sPreferences.getInt(PreferenceSearcheParam.RF_CALL_TIME,5);
		byte waitTimes = (byte) PreferenceCommon.sPreferences.getInt(PreferenceSearcheParam.RF_WAIT_TIME,30);
		byte callDistance = (byte) PreferenceCommon.sPreferences.getInt(PreferenceSearcheParam.RF_CALL_DISTANCE,12);
		byte freqChanel = (byte) PreferenceCommon.sPreferences.getInt(PreferenceSearcheParam.RF_COMM_FREQ,16);
		byte commFreqChanel = (byte) PreferenceCommon.sPreferences.getInt(PreferenceSearcheParam.RF_CALL_CHANEL,28);

		byte[] searchParamBytes = new byte[5];
		searchParamBytes[0] = callTimes;
		searchParamBytes[1] = waitTimes;
		searchParamBytes[2] = callDistance;
		searchParamBytes[3] = freqChanel;
		searchParamBytes[4] = commFreqChanel;

		return searchParamBytes;
	}
}
